/**
 * User: liutie
 * Date: 2019-05-18
 * Time: 11:43
 * Description: 此文件简要说明
 */


const Router = require('koa-router');
const sequelize = require('sequelize');
const querystring = require('querystring');

const gradeRouter = new Router();

const { AppUtil } = require('../util');
const {
    addToModel,
    updateModelById,
    associationModel,
    getAll,
    getDataByIdList,
    deleteById,
    deleteByIdWithAssociationModel,
} = require('../helper');

const validates = [{
    key: 'year',
    type: 'number',
}, {
    key: 'active',
    type: 'boolean'
}];

const {
    Grade
} = require('../models');

const {
    addGrade,
    deleteGrade,
    updateGrade,
    getGrade,
} = require('../actions/grade');

gradeRouter.post('/updateGrade', async (ctx) => {
    const body = ctx.request.body || {};
    const { isValid, message } = AppUtil.validateParams(validates, body);
    if (!isValid) {
        return ctx.body = message;
    }

    if (AppUtil.isEmpty(body.id)) {
        return await addGrade(body, ctx);
    }
    return await updateGrade(body, ctx);

})

gradeRouter.post('/deleteGrade', async (ctx) => {

})

gradeRouter.post('/getGrade', async (ctx) => {
    const body = ctx.request.body || {};
    if (AppUtil.isEmpty(body.id)) {
        return ctx.body = AppUtil.requestFailed('id不能为空');
    }

    return ctx.body = {
        success: true,
        data: await Grade.findByPk(body.id)
    }

})

module.exports = gradeRouter;